package String;

public class _844_BackspaceStringCompare {
    public boolean backspaceCompare(String S, String T) {
        String s1 = "";
        String s2 = "";
        int len1 = S.length() - 1;
        int len2 = T.length() - 1;
        int count1 = 0, count2 = 0;
        while (len1 >= 0) {
            if (S.charAt(len1) == '#') {
                count1++;
            } else {
                if (count1 > 0) {
                    count1--;
                } else {
                    s1 += S.charAt(len1);
                }
            }
            len1--;
        }
        while (len2 >= 0) {
            if (T.charAt(len2) == '#') {
                count2++;
            } else {
                if (count2 > 0) {
                    count2--;
                } else {
                    s2 += T.charAt(len2);
                }
            }
            len2--;
        }
        return s1.equals(s2);
    }
}
